﻿using SHomeWorkshop.LunarConcept;
using SHomeWorkshop.LunarConcept.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarMind
{
    /// <summary>
    /// ImagePresentationWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ImagePresentationWindow : Window
    {
        public ImagePresentationWindow()
        {
            InitializeComponent();

            Loaded += ImagePresentationWindow_Loaded;
            PreviewKeyDown += ImagePresentationWindow_PreviewKeyDown;

            Globals.SwitchInputMethod(false);
        }

        void ImagePresentationWindow_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.Tab:
                case Key.PageDown:
                    {
                        btnNext_Click(sender, e);
                        e.Handled = true; break;
                    }
                case Key.PageUp:
                    {
                        btnPreview_Click(sender, e);
                        e.Handled = true; break;
                    }
            }
        }

        private int point = 0;

        void ImagePresentationWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Presentate();
        }

        internal FrameworkElement xamlElement;

        private void Presentate()
        {
            if (titleImages.Count > 0 && (point >= 0 && point < titleImages.Count))
            {
                btnNext.Visibility = btnPreview.Visibility = System.Windows.Visibility.Visible;

                if (titleImages[point].ContentType == TitleImage.ImageType.BitmapImage)
                {
                    var bimg = titleImages[point].MainImage.Source as BitmapImage;
                    this.mainImage.Width = bimg.PixelWidth;
                    this.mainImage.Height = bimg.PixelHeight;
                    this.mainImage.Source = bimg;

                    this.mainImage.Visibility = System.Windows.Visibility.Visible;
                    if (this.baseViewBox.Child != this.mainImage) this.baseViewBox.Child = this.mainImage;
                }
                else if (titleImages[point].ContentType == TitleImage.ImageType.XamlElement)
                {
                    this.mainImage.Visibility = System.Windows.Visibility.Hidden;

                    try
                    {
                        var xamlText = titleImages[point].XamlText;

                        if (xamlText.StartsWith("<LunarConcept>") && xamlText.EndsWith("</LunarConcept>"))
                        {
                            PageEditorReader per = new PageEditorReader();
                            var conceptNode = titleImages[point].XmlData.SelectSingleNode(XmlTags.LunarConceptTag);
                            per.XmlData = conceptNode.SelectSingleNode(XmlTags.PageTag);
                            //per.Build();//设置XmlData属性值时会自动调用。

                            this.baseViewBox.Child = per;
                        }
                        else
                        {
                            if (string.IsNullOrWhiteSpace(xamlText))
                            {
                                if (this.baseViewBox.Child != this.mainImage)
                                    this.baseViewBox.Child = this.mainImage;
                                this.mainImage.Source = TitleImage.defaultBitmap;
                            }
                            else
                            {
                                byte[] array = System.Text.Encoding.UTF8.GetBytes(xamlText);

                                using (var stream = new System.IO.MemoryStream(array))
                                {
                                    this.xamlElement = System.Windows.Markup.XamlReader.Load(stream) as FrameworkElement;
                                    this.xamlElement.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                                    this.xamlElement.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                                    this.baseViewBox.Child = this.xamlElement;
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        if (this.baseViewBox.Child != this.mainImage)
                            this.baseViewBox.Child = this.mainImage;
                        this.mainImage.Source = TitleImage.defaultBitmap;
                    }

                    this.baseViewBox.Visibility = System.Windows.Visibility.Visible;
                }

                tbTitleText.Text = titleImages[point].Title;
                RefreshImageTitleText();

                this.Title = Globals.AppName + " [" + (point + 1) + " / " + titleImages.Count + "]";
            }
            else
            {
                this.Title = Globals.AppName;
            }
        }

        public void RefreshImageTitleText()
        {
            if (string.IsNullOrEmpty(tbTitleText.Text) == false)
            {
                tbTitleText.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                tbTitleText.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.F5://演示笔上只有F5
                case Key.F6://用F6开启，也用它关闭，这样很自然。
                case Key.Escape:
                    {
                        this.Close();
                        break;
                    }
                case Key.OemPlus:
                    {
                        this.sliderOfCanvasScale.Value += 1; break;
                    }
                case Key.OemMinus:
                    {
                        this.sliderOfCanvasScale.Value -= 1; break;
                    }
                case Key.D0:
                    {
                        this.sliderOfCanvasScale.Value = 0; break;
                    }
            }
        }

        private List<TitleImage> titleImages = new List<TitleImage>();

        public List<TitleImage> TitleImages { get { return this.titleImages; } }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {
            if (this.point >= titleImages.Count - 1)
            {
                //var result = MessageBox.Show("　　已经是最后一张了，要退出吗？", Globals.AppName, MessageBoxButton.YesNo, MessageBoxImage.Question);
                //if (result == MessageBoxResult.Yes)
                //{
                this.Close();
                //}
                return;
            }

            this.point++;

            Presentate();
        }

        private void btnPreview_Click(object sender, RoutedEventArgs e)
        {
            if (this.point <= 0)
            {
                //var result = MessageBox.Show("　　已经是第一张了，要退出吗？", Globals.AppName, MessageBoxButton.YesNo, MessageBoxImage.Question);
                //if (result == MessageBoxResult.Yes)
                //{
                this.Close();
                //}
                return;
            }

            this.point--;

            Presentate();
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
